package com.demo.webflux.spring5core.test.service;

import com.demo.webflux.spring5core.framework.annotation.Autowired;
import com.demo.webflux.spring5core.framework.annotation.Controller;
import com.demo.webflux.spring5core.framework.annotation.RequestMapping;
import com.demo.webflux.spring5core.framework.annotation.RequestParam;
import com.demo.webflux.spring5core.framework.webmvc.ModelAndView;
import com.demo.webflux.spring5core.mock.HttpServletRequest;
import com.demo.webflux.spring5core.mock.HttpServletResponse;

@Controller
@RequestMapping("/web")
public class MyAction {

	@Autowired
	private QueryService queryService;
	@Autowired
	private IModifyService iModifyService;

	@RequestMapping("/query.json")
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response, @RequestParam("name") String name) {
		String result = queryService.query(name);
		return out(response, result);
	}

	@RequestMapping("/add.json")
	public ModelAndView add(HttpServletRequest request, HttpServletResponse response, @RequestParam("name") String name, @RequestParam("addr") String addr) {
		String result = iModifyService.add(name, addr);
		return out(response, result);
	}

	@RequestMapping("/remove.json")
	public ModelAndView remove(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") Integer id) {
		String result = iModifyService.remove(id);
		return out(response, result);
	}

	@RequestMapping("/edit.json")
	public ModelAndView edit(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") Integer id, @RequestParam("name") String name) {
		String result = iModifyService.edit(id, name);
		return out(response, result);
	}

	private ModelAndView out(HttpServletResponse resp, String str) {
		try {
			resp.getWriter().write(str);
		}catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
